/**
 * May 5, 2014 4:59:07 PM
 */
package elitech.vietnam.copyweatherfashion.fragments.weekly;

import java.util.Calendar;
import java.util.Locale;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import elitech.vietnam.copyweatherfashion.LoginActivity;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.utilities.MyCallable;
import elitech.vietnam.copyweatherfashion.utilities.Utilities;
import elitech.vietnam.copyweatherfashion.weather.objects.WeatherData;

/**
 * @author Cong
 *
 */
public class WeeklyFragment extends Fragment {

	MainActivity mActivity;
	FragmentTabHost mTabHost;
	
	int mTask = 0;
	
	public WeeklyFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.weekly, container, false);
		
		mTabHost = (FragmentTabHost) view.findViewById(R.id.weekly_tabhost);
		
		mTabHost.setup(mActivity, getChildFragmentManager(), R.id.weekly_tabcontent);
		
		for (int i = 0; i < mActivity.mWeather.Data.size(); i++) {
			WeatherData data = mActivity.mWeather.Data.get(i);
			int selected = getTabIcon(data.Weathers.get(0).Description);
			int normal = getDateDrawable(i);
			mTabHost.addTab(mTabHost.newTabSpec(i + "").setIndicator(createTabView(selected, normal)),
					WeeklyContentFragment.class, WeeklyContentFragment.newArs(i));
		}
		
		return view;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		/*
		 * Initialize header view
		 */
		mActivity.mTxtTitle.setText(Html.fromHtml(getString(R.string.weeklytitle)));
		mActivity.mBtnBack.setVisibility(View.GONE);
		mActivity.mBtnCamera.setVisibility(View.GONE);
		mActivity.mBtnEdit.setVisibility(View.GONE);
		mActivity.mBtnNotify.setVisibility((mActivity.mUser!=null) ? View.VISIBLE : View.GONE);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		/**
		 * Implement onActivityResult Base on MainActivity.mRequestCode and
		 * MainActivity.mResultIntent
		 */
		if (mActivity.mRequestCode == MainActivity.REQUEST_LOGIN && mActivity.mResultIntent != null) {
			mActivity.getLoggedMemberInfo(mActivity.mResultIntent.getIntExtra(LoginActivity.EXTRA_USERID, -1),
					new MyCallable() {
						@Override
						public void call() {
							mActivity.mBtnNotify.setVisibility((mActivity.mUser != null) ? View.VISIBLE : View.GONE);
							mActivity.mRequestCode = -1;
							mActivity.mResultIntent = null;
						}
					});
		}
	}
	
	@Override
	public void onDestroyView() {
		mActivity.mWeeklyList1.clear();
		mActivity.mWeeklyList2.clear();
		mActivity.mWeeklyList3.clear();
		mActivity.mWeeklyList4.clear();
		mActivity.mWeeklyList5.clear();
		super.onDestroyView();
	}
	
	private View createTabView(int selected, int normal) {
		View view = LayoutInflater.from(mActivity).inflate(R.layout.weekly_tab, null);
		Drawable drawable = Utilities.getTabDrawable(mActivity, selected, normal);
		ImageView item_image = (ImageView) view.findViewById(R.id.weeklytab_imgImage);
		item_image.setImageDrawable(drawable);
		return view;
	}
	
	private int getTabIcon(String type) {
		if (type == null || type.equals(""))
			return R.drawable.tab_icon_trong;

		type = type.trim().toLowerCase(Locale.US);
		String[] listType = null;
		// trong veo
		listType = getResources().getStringArray(R.array.trong);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_trong;
		}
		// am_u
		listType = getResources().getStringArray(R.array.am_u);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_am_u;
		}
		// mua
		listType = getResources().getStringArray(R.array.mua);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua;
		}
		// mua_giong
		listType = getResources().getStringArray(R.array.mua_giong);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua_giong;
		}
		// mua_rao
		listType = getResources().getStringArray(R.array.mua_rao);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua_rao;
		}
		// mua_tuyet
		listType = getResources().getStringArray(R.array.mua_tuyet);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua_tuyet;
		}
		// nhieu_may
		listType = getResources().getStringArray(R.array.nhieu_may);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_nhieu_may;
		}
		// suong_mu
		listType = getResources().getStringArray(R.array.suong_mu);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_suong_mu;
		}
		// tuyet_bat_chot
		listType = getResources().getStringArray(R.array.tuyet_bat_chot);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_tuyet_bat_chot;
		}
		// rain
		if (type.contains("rain"))
			return R.drawable.tab_icon_mua;
		// rain
		if (type.contains("drizzle"))
			return R.drawable.tab_icon_mua;
		// snow
		if (type.contains("snow"))
			return R.drawable.tab_icon_tuyet;
		return R.drawable.tab_icon_trong;
	}
	
	private int getDateDrawable(int index) {
		Calendar cal = Calendar.getInstance();
		int n = cal.get(Calendar.DAY_OF_WEEK);
		int[] DATE_DRAWABLE = { R.drawable.weekly_img05_g, R.drawable.weekly_img05_a, R.drawable.weekly_img05_b, R.drawable.weekly_img05_c, R.drawable.weekly_img05_d, R.drawable.weekly_img05_e,
				R.drawable.weekly_img05_f };
		return DATE_DRAWABLE[((n + index <= 7) ? n + index : Math.abs(7 - n - index)) - 1];
	}
}
